package org.jaronsource.framework.core.service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.jaronsource.framework.core.dao.support.Criteria;
import org.jaronsource.framework.core.dao.support.ICommonDao;
import org.jaronsource.framework.core.dao.support.IEntity;
import org.jaronsource.framework.core.dao.support.Page;
import org.jaronsource.framework.core.dao.support.PageRequest;
import org.jaronsource.framework.core.dao.support.Sort;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Qualifier ( "commonService")
@Transactional
public class CommonService implements ICommonService {

	@Autowired
	protected ICommonDao commonDao;

	@Override
	public <T extends IEntity<I>, I extends Serializable> T save( T target ) {
		return commonDao.save( target );
	}

	@Override
	public <T extends IEntity<I>, I extends Serializable> T[] save( T... targets ) {
		return commonDao.save( targets );
	}
	
	@Override
	public <T extends IEntity<I>, I extends Serializable> void update( T target, I pk ) {
		commonDao.update( target, pk );
	}

	@Override
	public <T extends IEntity<I>, I extends Serializable> void remove(	Class<T> clazz,
																		I id ) {
		commonDao.remove( clazz, id );
	}
	
	@Override
	public <T extends IEntity<I>, I extends Serializable> void remove(	Class<T> clazz,
																		I... ids ) {
		commonDao.remove( clazz, ids );
	}

	@Override
	@Transactional ( readOnly = true)
	public <T extends IEntity<I>, I extends Serializable> boolean exists(	Class<T> clazz,
																			I id ) {
		return commonDao.exists( clazz, id );
	}

	@Override
	@Transactional ( readOnly = true)
	public <T extends IEntity<I>, I extends Serializable> T findReferenceByPk(	Class<T> clazz,
																				I id ) {
		return commonDao.findReferenceByPk( clazz, id );
	}

	@Override
	@Transactional ( readOnly = true)
	public <T extends IEntity<I>, I extends Serializable> T findByPk(	Class<T> clazz,
																		I id ) {
		return commonDao.findByPk( clazz, id );
	}

	@Override
	@Transactional ( readOnly = true)
	public <T extends IEntity<I>, I extends Serializable> T findOne(	Class<T> clazz,
																		Criteria criteria ) {
		return commonDao.findOne( clazz, criteria );
	}

	@Override
	@Transactional ( readOnly = true)
	public <T extends IEntity<I>, I extends Serializable> T findOne(	Class<T> clazz,
																		Sort sort,
																		Criteria criteria ) {
		return commonDao.findOne( clazz, sort, criteria );
	}

	@Override
	@Transactional ( readOnly = true)
	public <T extends IEntity<I>, I extends Serializable> T findOne(	Class<T> clazz,
																		String jpql,
																		Object... params ) {
		return commonDao.findOne( clazz, jpql, params );
	}

	@Override
	@Transactional ( readOnly = true)
	public <T extends IEntity<I>, I extends Serializable> T findOne(	Class<T> clazz,
																		String jpql,
																		Map<String, ?> params ) {
		return commonDao.findOne( clazz, jpql, params );
	}

	@Override
	@Transactional ( readOnly = true)
	public <T extends IEntity<I>, I extends Serializable> T findOne(	Class<T> clazz,
																		Sort sort,
																		String jpql,
																		Object... params ) {
		return commonDao.findOne( clazz, sort, jpql, params );
	}

	@Override
	@Transactional ( readOnly = true)
	public <T extends IEntity<I>, I extends Serializable> T findOne(	Class<T> clazz,
																		Sort sort,
																		String jpql,
																		Map<String, ?> params ) {
		return commonDao.findOne( clazz, sort, jpql, params );
	}

	@Override
	@Transactional ( readOnly = true)
	public <T extends IEntity<I>, I extends Serializable> List<T> findAll( Class<T> clazz ) {
		return commonDao.findAll( clazz );
	}

	@Override
	@Transactional ( readOnly = true)
	public <T extends IEntity<I>, I extends Serializable> List<T> find( Class<T> clazz,
																		Criteria criteria ) {
		return commonDao.find( clazz, criteria );
	}

	@Override
	@Transactional ( readOnly = true)
	public <T extends IEntity<I>, I extends Serializable> List<T> find( Class<T> clazz,
																		String jpql,
																		Object... params ) {
		return commonDao.find( clazz, jpql, params );
	}

	@Override
	@Transactional ( readOnly = true)
	public <T extends IEntity<I>, I extends Serializable> List<T> find( Class<T> clazz,
																		String jpql,
																		Map<String, ?> params ) {
		return commonDao.find( clazz, jpql, params );
	}

	@Override
	@Transactional ( readOnly = true)
	public <T extends IEntity<I>, I extends Serializable> List<T> find( Class<T> clazz,
																		Sort sort,
																		Criteria criteria ) {
		return commonDao.find( clazz, sort, criteria );
	}

	@Override
	@Transactional ( readOnly = true)
	public <T extends IEntity<I>, I extends Serializable> List<T> find( Class<T> clazz,
																		PageRequest pageRequest,
																		Criteria criteria ) {
		return commonDao.find( clazz, pageRequest, criteria );
	}

	@Override
	@Transactional ( readOnly = true)
	public <T extends IEntity<I>, I extends Serializable> List<T> find( Class<T> clazz,
																		Sort sort,
																		String jpql,
																		Object... params ) {
		return commonDao.find( clazz, sort, jpql, params );
	}

	@Override
	@Transactional ( readOnly = true)
	public <T extends IEntity<I>, I extends Serializable> List<T> find( Class<T> clazz,
																		Sort sort,
																		String jpql,
																		Map<String, ?> params ) {
		return commonDao.find( clazz, sort, jpql, params );
	}

	@Override
	@Transactional ( readOnly = true)
	public <T extends IEntity<I>, I extends Serializable> List<T> find( Class<T> clazz,
																		PageRequest pageRequest,
																		String jpql,
																		Object... params ) {
		return commonDao.find( clazz, pageRequest, jpql, params );
	}

	@Override
	@Transactional ( readOnly = true)
	public <T extends IEntity<I>, I extends Serializable> List<T> find( Class<T> clazz,
																		PageRequest pageRequest,
																		String jpql,
																		Map<String, ?> params ) {
		return commonDao.find( clazz, pageRequest, jpql, params );
	}

	@Override
	@Transactional ( readOnly = true)
	public <T extends IEntity<I>, I extends Serializable> long count(	Class<T> clazz,
																		Criteria criteria ) {
		return commonDao.count( clazz, criteria );
	}

	@Override
	@Transactional ( readOnly = true)
	public long count( String jpql, Object... params ) {
		return commonDao.count( jpql, params );
	}

	@Override
	@Transactional ( readOnly = true)
	public long count( String jpql, Map<String, ?> params ) {
		return commonDao.count( jpql, params );
	}

	@Override
	@Transactional ( readOnly = true)
	public <T extends IEntity<I>, I extends Serializable> Page<T> findPage( Class<T> clazz,
																			PageRequest pageRequest,
																			Criteria criteria ) {
		return commonDao.findPage( clazz, pageRequest, criteria );
	}

	@Override
	@Transactional ( readOnly = true)
	public <T extends IEntity<I>, I extends Serializable> Page<T> findPage( Class<T> clazz,
																			PageRequest pageRequest,
																			String jpql,
																			Object... params ) {
		return commonDao.findPage( clazz, pageRequest, jpql, params );
	}

	@Override
	@Transactional ( readOnly = true)
	public <T extends IEntity<I>, I extends Serializable> Page<T> findPage( Class<T> clazz,
																			PageRequest pageRequest,
																			String jpql,
																			Map<String, ?> params ) {
		return commonDao.findPage( clazz, pageRequest, jpql, params );
	}

	@Override
	@Transactional ( readOnly = true)
	public <X> List<X> query( String jpql, Object... params ) {
		return commonDao.query( jpql, params );
	}

	@Override
	@Transactional ( readOnly = true)
	public <X> List<X> query( String jpql, Map<String, ?> params ) {
		return commonDao.query( jpql, params );
	}

	@Override
	@Transactional ( readOnly = true)
	public <X> List<X> query( Sort sort, String jpql, Object... params ) {
		return commonDao.query( sort, jpql, params );
	}

	@Override
	@Transactional ( readOnly = true)
	public <X> List<X> query( Sort sort, String jpql, Map<String, ?> params ) {
		return commonDao.query( sort, jpql, params );
	}

	@Override
	@Transactional ( readOnly = true)
	public <X> List<X> query(	PageRequest pageRequest,
								String jpql,
								Object... params ) {
		return commonDao.query( pageRequest, jpql, params );
	}

	@Override
	@Transactional ( readOnly = true)
	public <X> List<X> query(	PageRequest pageRequest,
								String jpql,
								Map<String, ?> params ) {
		return commonDao.query( pageRequest, jpql, params );
	}

	@Override
	@Transactional ( readOnly = true)
	public <X> X queryOne( String jpql, Object... params ) {
		return commonDao.queryOne( jpql, params );
	}

	@Override
	@Transactional ( readOnly = true)
	public <X> X queryOne( String jpql, Map<String, ?> params ) {
		return commonDao.queryOne( jpql, params );
	}

	@Override
	@Transactional ( readOnly = true)
	public <X> X queryOne( Sort sort, String jpql, Object... params ) {
		return commonDao.queryOne( sort, jpql, params );
	}

	@Override
	@Transactional ( readOnly = true)
	public <X> X queryOne( Sort sort, String jpql, Map<String, ?> params ) {
		return commonDao.queryOne( sort, jpql, params );
	}

	@Override
	@Transactional ( readOnly = true)
	public <X> Page<X> queryPage(	PageRequest pageRequest,
									String jpql,
									Object... params ) {
		return commonDao.queryPage( pageRequest, jpql, params );
	}

	@Override
	@Transactional ( readOnly = true)
	public <X> Page<X> queryPage(	PageRequest pageRequest,
									String jpql,
									Map<String, ?> params ) {
		return commonDao.queryPage( pageRequest, jpql, params );
	}

	@Override
	public int execute( String jpql, Object... params ) {
		return commonDao.execute( jpql, params );
	}

	@Override
	public int execute( String jpql, Map<String, ?> params ) {
		return commonDao.execute( jpql, params );
	}

}
